package com.qcby.file.controller;


import com.qcby.common.core.domain.Result;
import com.qcby.system.api.domain.SysFileDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.qcby.common.core.utils.file.FileUtils;
import com.qcby.file.service.ISysFileService;

import javax.annotation.Resource;

/**
 * 文件请求处理
 *
 * @author qcby
 */
@RestController
@Slf4j
public class SysFileController {

    @Resource
    private ISysFileService sysFileService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public Result<SysFileDTO> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFileDTO sysFile = new SysFileDTO();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return Result.getSuccessResult(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return Result.getServiceError("上传文件失败");
        }
    }
}